package com.bawei.mdm2207alast.mine.ask

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.bawei.mdm2207alast.base.OnError
import com.bawei.mdm2207alast.base.OnLoading
import com.bawei.mdm2207alast.base.OnSuccess
import com.bawei.mdm2207alast.common.BaseTitle
import com.bawei.mdm2207alast.common.UserCard
import com.bawei.mdm2207alast.model.db.AskEntity
import com.blankj.utilcode.util.ToastUtils
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat

/**
 * 作者：赵亮
 * 时间：2024/12/24 11:07
 * 功能：请假页面
 */
@Composable
fun AskCommitPage(
    navController: NavController = rememberNavController(),
    viewModel: AskViewModel = hiltViewModel()
) {

    // 请假类型
    var type by remember {
        mutableStateOf("事假")
    }

    // 开始时间
    var start by remember {
        mutableStateOf("2024-12-24")
    }

    // 结束时间
    var end by remember {
        mutableStateOf("2024-12-28")
    }

    var reason by remember {
        mutableStateOf("")
    }

    LaunchedEffect(key1 = "") {
        launch {
            viewModel.uiState.collect {
                when(it) {
                    is OnError -> {}
                    OnLoading -> {}
                    is OnSuccess<*> -> {
                        ToastUtils.showShort("提交成功")
                        navController.popBackStack()
                    }
                }
            }
        }
    }

    Column {
        // 标题
        BaseTitle(navController = navController, title = "请假申请")
        Column(
            modifier = Modifier.padding(start = 15.dp, end = 15.dp, top = 10.dp),
            verticalArrangement = Arrangement.spacedBy(10.dp)
        ) {
            // 员工信息
            UserCard(navController = navController, mmkv = MMKV.defaultMMKV())

            // 请假类型
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(text = "请假类型:")
                RadioButton(selected = type == "事假", onClick = { type = "事假" })
                Text(text = "事假")
                RadioButton(selected = type == "病假", onClick = { type = "病假" })
                Text(text = "病假")
            }

            // 开始时间
            Row(
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.spacedBy(10.dp)
            ) {
                Text(text = "开始时间:")
                OutlinedTextField(
                    value = start,
                    onValueChange = { start = it },
                    modifier = Modifier.fillMaxWidth()
                )
            }

            // 结束时间
            Row(
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.spacedBy(10.dp)
            ) {
                Text(text = "结束时间:")
                OutlinedTextField(
                    value = end,
                    onValueChange = { end = it },
                    modifier = Modifier.fillMaxWidth()
                )
            }

            // 请假原因
            Row(
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.spacedBy(10.dp)
            ) {
                Text(text = "请假原因:")
                OutlinedTextField(
                    modifier = Modifier.fillMaxWidth(),
                    value = reason,
                    onValueChange = { reason = it })
            }
            // 提交按钮
            Button(modifier = Modifier.fillMaxWidth(), onClick = {
                // 发送提交请假的意图
                viewModel.sendIntent(
                    AskIntent.CommitAsk(
                        AskEntity(
                            0,
                            MMKV.defaultMMKV().decodeLong("uid"),
                            type,
                            reason,
                            start,
                            end,
                            SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(System.currentTimeMillis()),
                            "",
                            "已提交"

                        )
                    )
                )
            }) {
                Text(text = "提交")
            }
        }
    }
}

@Preview(showSystemUi = true)
@Composable
fun AskCommitPagePreview() {
    AskCommitPage()
}

